import Nprogress from "nprogress";
import router from "../router/index";
import "nprogress/nprogress.css";
import { isLogin } from "@/utils/user";
// import { SDKyanzheng, fenxiang } from '../jcui/wx-api'

function hasLoginState(to) {
  if ((to.meta.login || to.path === "/gedanlist/我的收藏") && !isLogin()) {
    Qmsg.info(`${to.meta.title}页面需要登录`);
    router.push({
      name: "Login",
      query: { redirect: to.path },
    });
    return true;
  }
  return false;
}

router.beforeEach(async (to, from, next) => {
  Nprogress.start();
  // 登录检测
  // const loginState = hasLoginState()
  // if (!loginState) {
  //   try {
  //     // 如果当前是微信浏览器进入网页授权
  //     if (isWeiXin()) {
  //       try {
  //         const weixinLoginState = await weixinGetRedirectResult()
  //         if (weixinLoginState) {
  //           store.commit('SET_LOGIN_STATE', weixinLoginState)
  //           await addUserInfo(weixinLoginState.user)
  //         } else {
  //           weixinLogin()
  //         }
  //       } catch {
  //         weixinLogin()
  //       }
  //     } else {
  //       await anonymousAuthProvider()
  //       store.commit('SET_LOGIN_STATE', hasLoginState())
  //       await addUserInfo(hasLoginState().user)
  //     }
  //   } catch (e) {
  //     // eslint-disable-next-line no-console
  //     console.log(e)
  //     msg({ type: 'error', msg: store.getters.t('from.failure') })
  //     return
  //   }
  // } else if (!store.getters.GET_LOGIN_TYPE) {
  //   Auth.currentUser.refresh().then(() => {
  //     store.commit('SET_LOGIN_STATE', hasLoginState())
  //     addUserInfo(hasLoginState().user)
  //   })
  // } else {
  //   store.commit('SET_LOGIN_STATE', loginState)
  // }

  if (hasLoginState(to)) {
    return;
  }

  document.title = to.meta.title;
  next();
});
router.afterEach((to) => {
  const newto = { ...to };
  delete newto.matched;
  Nprogress.done();
});
